/**
 * RemoteObserver.java
 */
package fss.remote;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Observable;
import java.util.Observer;

import fss.util.rmi.RmiUtils;

/**
 * This is a wrapper for all service objects so that they can be registered
 * with Java Observable/Observer framework.
 * 
 * @author thinh
 *
 */
public class RemoteObserver extends UnicastRemoteObject implements Observer, Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * The wrapped observer object.
     */
    private RemoteService wrapped = null;
    /**
     * Wrapping a remote service as an adaptor to Java 
     * Observable/Observer framework.
     * @param service the wrapped observer object.
     */
    public RemoteObserver(RemoteService service) throws RemoteException {
        wrapped = service;
    }
    @Override
    public void update(Observable observable, Object message) {
        try {
            RemoteService service = (RemoteService) RmiUtils.getRemoteObject(wrapped.getId());
            service.update(observable, message);
        } catch (RemoteException e) {
            observable.deleteObserver(this);
        }
    }
}
